####################################################################
# Replication code for
#   Lyall, Shiraito, and Imai, "Coethnic Bias and Wartime Informing"
#
# Code to create Figure 2
#
# Author: Yuki Shiraito
# Created: March 2, 2015
####################################################################

load("fig2_dat.RData")

### Figure 2: Distribution of Responses from the Endorsement Experiment by Ethnic Groups and Endorsements.
yaxis <- c("ISAF", "Pashtun", "Tajik")
col.seq <- c(grey(seq(0.3^2.2, 0.9^2.2, length = 5)^(1/2.2)), rep(rgb(0,0,0,0), 2)) 

pdf("EthnicDescriptiveByEthnic.pdf", height = 6, width = 7.2)

par(mfcol = c(4,2), las = 1, mar=c(0,3,0.5,0.25), oma=c(6,6,6,0.5), cex = 0.55)

#### Tajik
barplot(t(rbind(matrix(prop.table(table(data.tajik$call_ff)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$call_pashtun)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$call_tajik)[c(5:1, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = yaxis, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = rgb(.31, .31, .31), cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.tajik$call_ff)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$call_pashtun)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$call_tajik)[c(5:1, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = yaxis, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = col.seq, add = TRUE, cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.tajik$anon_ff)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$anon_pashtun)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$anon_tajik)[c(5:1, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = yaxis, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = rgb(.31, .31, .31), cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.tajik$anon_ff)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$anon_pashtun)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$anon_tajik)[c(5:1, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = yaxis, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = col.seq, add = TRUE, cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.tajik$dropin_ff)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$dropin_pashtun)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$dropin_tajik)[c(5:1, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = yaxis, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = rgb(.31, .31, .31), cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.tajik$dropin_ff)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$dropin_pashtun)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$dropin_tajik)[c(5:1, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = yaxis, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = col.seq, add = TRUE, cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.tajik$retal_ff)[c(1:5, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$retal_pashtun)[c(1:5, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$retal_tajik)[c(1:5, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = yaxis, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = rgb(.31, .31, .31), cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.tajik$retal_ff)[c(1:5, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$retal_pashtun)[c(1:5, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.tajik$retal_tajik)[c(1:5, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = yaxis, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = col.seq, add = TRUE, cex.names = 1.7) 

#### Pashtun
barplot(t(rbind(matrix(prop.table(table(data.pashtun$call_ff)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$call_pashtun)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$call_tajik)[c(5:1, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = NULL, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = rgb(.31, .31, .31), cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.pashtun$call_ff)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$call_pashtun)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$call_tajik)[c(5:1, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = NULL, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = col.seq, add = TRUE, cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.pashtun$anon_ff)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$anon_pashtun)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$anon_tajik)[c(5:1, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = NULL, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = rgb(.31, .31, .31), cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.pashtun$anon_ff)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$anon_pashtun)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$anon_tajik)[c(5:1, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = NULL, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = col.seq, add = TRUE, cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.pashtun$dropin_ff)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$dropin_pashtun)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$dropin_tajik)[c(5:1, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = NULL, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = rgb(.31, .31, .31), cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.pashtun$dropin_ff)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$dropin_pashtun)[c(5:1, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$dropin_tajik)[c(5:1, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = NULL, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = col.seq, add = TRUE, cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.pashtun$retal_ff)[c(1:5, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$retal_pashtun)[c(1:5, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$retal_tajik)[c(1:5, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = NULL, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = rgb(.31, .31, .31), cex.names = 1.7) 

barplot(t(rbind(matrix(prop.table(table(data.pashtun$retal_ff)[c(1:5, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$retal_pashtun)[c(1:5, 6, 7)]), nrow = 1),
                matrix(prop.table(table(data.pashtun$retal_tajik)[c(1:5, 6, 7)]), nrow = 1))),
        horiz = TRUE, xaxt = "n", names.arg = NULL, density = c(rep(-1, 5), rep(25, 2)),
        angle = c(rep(-45, 6), 45), lwd = 1.25, col = col.seq, add = TRUE, cex.names = 1.7) 

par(xpd=NA)
legend(-.2, .3, c(" Certain", " Likely", " Might", " Unlikely", " Will not",
                " Refused", " Don't Know"),
       density = c(rep(-1, 5), rep(40, 2)), angle = c(rep(-45, 6), 45), cex = 1.5,
       fill = c(col.seq[1:5], rep(rgb(.31, .31, .31), 2)), horiz=T, xjust=0.5,
       x.intersp = 0.15, bty = "n")

n.res.tajik <- nrow(data.tajik)
mtext(paste("Tajik respondents (N =", n.res.tajik, ")", sep = ""), outer = TRUE, at = 0.28,
      cex = 1, line = 1)
n.res.pashtun <- nrow(data.pashtun)
mtext(paste("Pashtun respondents (N =", n.res.pashtun, ")", sep = ""), outer = TRUE, at = 0.78,
      cex = 1, line = 1)

par(las = 0)
mtext("Guardians", outer = TRUE, side = 2, line = 4.2, at = 0.87 - 0.24*(1-1), cex = 1.2)
mtext("Anonymity", outer = TRUE, side = 2, line = 4.2, at = 0.85 - 0.24*(2-1), cex = 1.2)
mtext("Stopping by", outer = TRUE, side = 2, line = 4.2, at = 0.85 - 0.24*(3-1), cex = 1.2)
mtext("Retaliation", outer = TRUE, side = 2, line = 4.2, at = 0.85 - 0.24*(4-1), cex = 1.2)
lines(c(-2.5, 1.1), rep(3.9, 2), lty = 2)

dev.off()
